package com.thghh.map;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.keyvalue.MultiKey;
import org.apache.commons.collections4.map.MultiKeyMap;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author pengzhikang
 * @date 2021/11/17 16:34
 * @since 1.0.0
 */
public class MapGroupTest {

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class P {
        int id;
        String name;
        int age;
        String sex;
    }

    public static void main(String[] args) {

        List<P> list = new ArrayList<>();
        list.add(new P(1, "peng", 2, "男"));
        list.add(new P(2, "peng", 4, "女"));
        list.add(new P(1, "peng", 2, "男"));
        list.add(new P(2, "peng", 4, "女"));
        list.add(new P(1, "liu", 2, "男"));
        list.add(new P(2, "liu", 4, "女"));
        list.add(new P(1, "liu", 2, "男"));
        list.add(new P(2, "liu", 4, "女"));

        MultiKeyMap<String, List<P>> multiKeyMap = list.stream().collect(Collectors.groupingBy(p -> new MultiKey<String>(p.name, p.sex), MultiKeyMap::new, Collectors.toList()));
        multiKeyMap.values().forEach(p -> System.out.println(p));

        System.out.println("--------------------------");
        System.out.println(multiKeyMap.get("peng", "男"));

    }
}
